package com.iskyonline.skyonlieapp.controller;

import com.iskyonline.skyonlieapp.dto.ResponseDTO;
import com.iskyonline.skyonlieapp.exception.BizException;
import com.iskyonline.skyonlieapp.service.IndexService;
import com.iskyonline.skyonlieapp.util.IDUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;

@Controller
public class IndexController extends BaseController {

    @Autowired
    private IndexService indexService;

    @RequestMapping("/")
    public String toIndex(HttpServletRequest request) {
//        requstSave(request, indexService);
        return "redirect:/home.html";
    }

    @RequestMapping("/home.html")
    public String toIndex2(HttpServletRequest request) {
        requstSave(request, indexService);
        return "home";
    }

    @RequestMapping("/bo/common/upload.html")
    @ResponseBody
    public ResponseDTO boCommonUploadPicture(MultipartFile upfile, HttpSession session, HttpServletResponse response) {
        try {
            if (upfile.isEmpty()) {
                return ResponseDTO.failed("上传图片失败");
            }

            String fileName = IDUtils.createImageName("product");
            File uploadedFile = new File(filePath + fileName);
            upfile.transferTo(uploadedFile);

            Map<String, String> map = new HashMap<>(2);
            map.put("filename", fileName);
            map.put("imageUrl", fileUrl + fileName);

            return ResponseDTO.success(map);

        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("上传图片失败");
        }
    }

    @RequestMapping("/bo/homePicture/upload.html")
    @ResponseBody
    public ResponseDTO boHomeUploadPicture(MultipartFile upfile, HttpSession session, HttpServletResponse response) {
        try {
            if (upfile.isEmpty()) {
                return ResponseDTO.failed("上传图片失败");
            }

            String fileName = IDUtils.createImageName("product");
            File uploadedFile = new File(filePath + fileName);
            upfile.transferTo(uploadedFile);

            Map<String, String> map = new HashMap<>(2);
            map.put("filename", fileName);
            map.put("imageUrl", fileUrl + fileName);

            indexService.saveIndexPicture(fileName);

            return ResponseDTO.success(map);

        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("上传图片失败");
        }
    }

    @RequestMapping(value = "/fld/images/{filename}", method = RequestMethod.GET)
    @ResponseBody
    public void showImage(@PathVariable("filename") String filename, HttpServletResponse response) {

        response.setContentType("image/png");
        try(BufferedInputStream bis = new BufferedInputStream(new FileInputStream(super.filePath + filename));
            OutputStream os = response.getOutputStream()) {
            byte[] buff = new byte[1024];
            int length = -1;
            while (-1 != (length=bis.read(buff))) {
                os.write(buff, 0, length);
            }
            os.flush();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @RequestMapping("/bo/saveHomeBanner")
    @ResponseBody
    public ResponseDTO saveHomeBanner(String picTitle, String picUrl) {
        indexService.saveIndexBannerInfo(picUrl, picTitle);
        return ResponseDTO.success();
    }

    @RequestMapping("/bo/saveHeZuoHuoBan")
    @ResponseBody
    public ResponseDTO saveHeZuoHuoBan(String picUrl) {
        indexService.saveHeZuoHuoBan(picUrl);
        return ResponseDTO.success();
    }

    @RequestMapping("/listIndexBanner")
    @ResponseBody
    public ResponseDTO listIndexBanner() {
        return ResponseDTO.success(indexService.listKeyValue(2));
    }

    @RequestMapping("/listHeZuoHuoBan")
    @ResponseBody
    public ResponseDTO listHeZuoHuoBan() {
        return ResponseDTO.success(indexService.listKeyValue(3));
    }

    @RequestMapping("/bo/del")
    @ResponseBody
    public ResponseDTO del(int id) {
        indexService.delKeyValue(id);
        return ResponseDTO.success();
    }

    @RequestMapping("/bo/saveWaiBaoFuwu")
    @ResponseBody
    public ResponseDTO saveWaiBaoFuwu(String context, int valueType) {
        indexService.saveWaiBao(context, valueType);
        return ResponseDTO.success();
    }

    @RequestMapping("/bo/loadKeyValueByType")
    @ResponseBody
    public ResponseDTO loadKeyValueByType(int valueType) {
        return ResponseDTO.success(indexService.loadKeyValueByType(valueType));
    }

    @RequestMapping("/bo/saveWebSiteInfo")
    @ResponseBody
    public ResponseDTO saveWebSiteInfo(String picUrl, String address, String beianhao, String phone) {
        indexService.saveInfo(picUrl, address, beianhao, phone);
        return ResponseDTO.success();
    }

}
